/*!
 * Ext JS Library 3.0.0
 * Copyright(c) 2006-2009 Ext JS, LLC
 * licensing@extjs.com
 * http://www.extjs.com/license
 */
/*
*** (C) 2008 UCMai.com ***

Subj: Module
Auth: zhouyongtai
Date: 2010-12-13
*/

var site_store = new Ext.data.ArrayStore({
	fields: [
		{ name: 'name' },
		{ name: 'no'},
		{ name: 'url'},
		{ name: 'img'}
	]
});
var site_data = [
	['buymssoftware.com', "20110001", "http://www.buymssoftware.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/buymssoftware.gif"],
	['bkshoesonsale.com', "20110002", "http://www.bkshoesonsale.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/bkshoesonsale.gif"],
	['coachoutletshophandbags.com', "20110003", "http://www.coachoutletshophandbags.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/coachoutletsshophandbags.gif"],
	['cheaplovejewelry.com', "20110004", "http://www.cheaplovejewelry.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/cheaplovejewelry.gif"],
	['cheapesthandbagsforsale.com', "20110005", "http://www.cheapesthandbagsforsale.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/cheapesthandbagsforsale.gif"],
	['buyoutlook2010.com', "20110006", "http://www.buyoutlook2010.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/buyoutlook2010.gif"],
	['professionalplus2010.com', "20110007", "http://www.professionalplus2010.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/professionalplus2010.gif"],
	['microsoftoffice2010online.com', "20110008", "http://www.microsoftoffice2010online.com/admin/privilege.php?act=login&key=dc48b26405fbfbb8", "logo/microsoft2010online.gif"]
];

site_store.loadData(site_data);
var site_list =  {
	style:"background-color:red",
	layout : "fit",
	iconCls: 'new-tab',
	tbar:[{text:'进入站点', iconCls: 'open'},'-',{text:'查询条件', iconCls: 'search'},'-',{xtype:'textfield',id:"sitename", name:'sitename'},'-',{text:'站点订单快速查询',iconCls:'search', handler:function(){
		var desktop = MyDesktop.getDesktop();
		var win = desktop.getWindow('order_fast_query-win');
		var p = new Ext.Panel({
					
		});
		if(!win){
			win = desktop.createWindow({
				id: 'order_fast_query-win',
				title:'站点订单快速查询',
				width:600,
				height:300,
				items:p,
				iconCls: 'grid',
				layout: 'fit',
				shim:false,
				animCollapse:false,
				constrainHeader:true,
				tbar:['订单号','-',{xtype:'textfield',id:"order_sn", name:'order_sn'},'-',{text:'查询',iconCls:'search',handler:function(){
					showLoading("正在验证，请稍侯...","正在查询中...");
					zyt.ajax('', {}, function() {
						hideLoading();
						top.location = config.redirect;
						//window.location.href = config.redirect;
					}.createDelegate(this));
				}}]
			});
		}
		win.show();
	}}],
	items: new Ext.DataView({
		id : "food_list_list",
		store : site_store,
		tpl : new Ext.XTemplate(
				'<tpl for=".">',
					'<div class="thumb-wrap" id="{name}" no="{no}" href="{url}">',
					'<div class="thumb"><img src="{img}" title="{name}"></div>',
					'</div>',
				'</tpl>',
				'<div class="x-clear"></div>'
		),
		split : true,
		style : "overflow:auto",
		multiSelect : true,
		itemSelector : 'div.thumb-wrap',
		emptyText: 'No Site to display',
		listeners : {
			"dblclick":{fn:function(ev,i,el,e){
				var ds = ev.getSelectedRecords();
			
				var site = {};
				site.id = el.id;
				for(var i=0;i<ds.length;i++){
					var d = ds[i].data;
					site.no = d.no;
					site.url = d.url;
				}

				var desktop = MyDesktop.getDesktop();
				var win = desktop.getWindow(site.no);
				if(!win){
					win = desktop.createWindow({
						id: site.no,
						title:site.id,
						width:800,
						height:500,
						items:new Ext.Panel({
							id:'load_site',
							border:false,
							layout:'fit',
							html:'<iframe src="'+site.url+'" marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" width="100%" height="100%"></iframe>'
						}),
						//html : '<p>Something useful would be in here.</p>',
						iconCls: 'view',
						layout: 'fit',
						shim:false,
						animCollapse:false,
						constrainHeader:true
					});
				}
				win.show();
			},scope:this}
		}
	}),
	bbar: new Ext.PagingToolbar( {
		pageSize : zyt.isNull(this.pageSize,c.page.LargePageSize),
		store : site_store,
		displayInfo : true,
		displayMsg : c.page.pgholder,
		emptyMsg : c.page.nodata,
		items : [ '-' ]
	})
};

Ext.app.Module = function(config){
	Ext.apply(this, config);
	Ext.app.Module.superclass.constructor.call(this);
	this.init();
}

Ext.extend(Ext.app.Module, Ext.util.Observable, {
	init : Ext.emptyFn,
	createWindow : function(src){
		var desktop = MyDesktop.getDesktop();
		var win = desktop.getWindow(src.windowId);
		
		MyDesktop.importMod(src.url,function(){
			//var item = {};
			eval('var item = ' + src.item);
			if(!win){
				win = desktop.createWindow({
					id: src.windowId,
					title:src.text,
					width:800,
					height:500,
					items:new item(),
					//html : '<p>Something useful would be in here.</p>',
					iconCls: src.iconCls,
					layout: 'fit',
					shim:false,
					//manager:userWins,
					animCollapse:false,
					constrainHeader:true
				});
			}
			win.show();
		});
	},
	onContextmenu:function(src){
		alert(src.text);
	}
});

var MyDesktop = new Ext.app.App({
	init :function(){
		
		Ext.QuickTips.init(); //初始化验证提示
	},
	
	getModules : function(){
		var trees = [];
		var windowIndex = -1;
		this.ajax('profile/1SF.aspx','',function(ts){
			var items = eval(ts)
			for(var i=0;i<items.length;i++) {			
				var	node = Ext.extend(Ext.app.Module, {
					//id:'icon-grid',
					init : function(){
						var menuItems = [];
						if(items[i].children) {
							var subItems = items[i].children;
							for(var j=0;j<subItems.length;j++) {
								menuItems.push({
									text:subItems[j].text,
									iconCls:subItems[j].iconCls,
									handler:this.createWindow,
									windowId: subItems[j].id ,//+ (++windowIndex),
									scope: this,
									url:subItems[j].url,
									item:subItems[j].item//,
									//listeners : {
									//	"contextmenu" :{fn:this.onContextmenu,scope:this}
									//}
								});
							}
							this.launcher = {
								text: items[i].text,
								iconCls: items[i].iconCls,
								handler: function() {
									return false;
								},
								menu: {
									items:menuItems
								}
							}
						}else{
							this.launcher = {
								text: items[i].text,
								iconCls:items[i].iconCls,
								handler : this.createWindow,
								scope: this,
								url:items[i].url,
								item:items[i].item,
								windowId:items[i].id//, //+ (++windowIndex)
								//listeners : {
								//	"contextmenu" :{fn:this.onContextmenu,scope:this}
								//}
								
							}
						}
					}
				});
				trees.push(new node());
			}
			trees.push(shortClickWin({item : site_list, windowId : 'website_win', title : '站点管理', iconCls : 'icon-grid'}));
			MyDesktop.modules =  trees;//this.getModules();
			if(MyDesktop.modules){
				MyDesktop.initModules(MyDesktop.modules);
			}
			
			Ext.get('loading').remove();
			Ext.get('loading-mask').fadeOut({remove:true});
		});
	},

	// config for the start menu
	getStartConfig : function(){
        return {
            title: '<b style="color:red">' + zyt.cookies.get('aln') + '</b> Administrator',
            iconCls: 'user',
            toolItems: [{
				text:c.user.Infor,
				iconCls:'user',
				scope:this,
				handler: MyDesktop.sysFun.UserInfor
	
			},{
				text:c.user.CPass,
				handler:MyDesktop.sysFun.UserCPass,
				iconCls:'user',
				scope:this
			},{
				text:c.user.Toolkit,
				iconCls:'user',
				scope:this,
				menu:new Ext.menu.Menu({items:[{text:'MSN'}]})
			},{
				text:c.theme,
				iconCls:'theme',
				scope:this,
				menu:new Ext.menu.Menu({
						items:[{
							text:'默认主题',
							handler:function(){
								zyt.cookies.set('UCM_THEME','');
								zyt.setStyle('');
							}
						},
						{
							text:'绿色主题',
							handler:function(){
								zyt.cookies.set('UCM_THEME','green');
								zyt.setStyle('green');
							}
						},
						{
							text:'兰色主题',
							handler:function(){
								zyt.cookies.set('UCM_THEME','blue');
								zyt.setStyle('blue');
							}
						}]
					})
			},'-',{
                text:c.setting,
                iconCls:'settings',
                scope:this, 
				handler:function(){Ext.Msg.alert('','构建中...')}
            },{
				text:c.HelpSupport,
				iconCls:'help',
				scope:this,
				handler:function(){Ext.Msg.alert('','构建中...')}
				
			},'-',{
                text:c.logout,
                iconCls:'logout',
                scope:this,
				handler:function(){
					showLoading('系统正在注销，请稍后...','正在注销中...');
					zyt.ajax(serverUrl_Login + 'logout',{action:'logout'}, function() {
						window.location.href = '/login.html';
					});
				}
            }]
        };
    }
});

MyDesktop.sysFun = {
	UserInfor: function(o){
		MyDesktop.desktop.createNoBWin({
					id:'ddd',
					title:'ddd',
					width:300,
					height:300,
					//items:[],
					html : '<p>Something useful would be in here.</p>',
					shim:false,
					animCollapse:false,
					constrainHeader:true});
	},
	UserCPass : function(o){
		Ext.Msg.alert(o.text);
	}
};


function shortClickWin (config){
	MyDesktop.GridWindow = Ext.extend(Ext.app.Module, {
		init : function(){
			this.launcher = {
				text: config.title,
				iconCls: config.iconCls,
				handler : this.createWindow,
				scope: this,
				noStart : true,
				item : config.item , 
				//item:items[i].item,
				windowId : config.windowId //, //+ (++windowIndex)
				//listeners : {
				//	"contextmenu" :{fn:this.onContextmenu,scope:this}
				//}
			}
		}
	});
	return new MyDesktop.GridWindow();
}